// define the namespace
syslib.namespace("syslib.ui");

// tag conversor
syslib.ui.DialogTag = __class(syslib.ui.TagConversor, {
	dialog : null,
	
	execute : function(){
		// create the dialog
		var dialog = new syslib.ui.Dialog(__extend(this.__attributes, {
			target: false, 
			title : (__valid(this.__attributes.title)) ? this.__attributes.title : "Untitled dialog"
		}));
		
		// append
		dialog.show();
		
		// store the dialog
		this.dialog = dialog;
		
		// return the dialog element
		return this.__element;
	}, 
	
	getComponent : function(){
		return this.dialog;
	}
});

// define the component
syslib.ui.Dialog = __class(syslib.ui.UIComponent, {
	__options : null, 
	references : null, 
	
	__construct : function(options){
		// set the options
		this.__options = __extend({
			id : syslib.element.generateID(),  
			title : "Dialog", 
			content : "", 
			contentElement : null, 
			target : document.body, 
			enableDrag : true, 
			style : "", 
			bodyStyle : "", 
			fullscreen: false, 
			buttons : []
		}, options);
	
		// load the style
		syslib.loadStyleForComponent("dialog");
	}, 
	
	__createElement : function(){
		// create the structure
		var elemtJSON = ["div", {"class" : "syslib-dialog-container syslib-border", "id" : this.__options.id, "style" : this.__options.style},
			["div", {"class" : "syslib-3d-background  syslib-bottom-border", "innerName" : "titleBar"},
				["span", {"class" : "syslib-title-text syslib-dialog-title", "innerName" : "title"}, 
					this.__options.title
				], 
				["div", {"class" : "syslib-dialog-close-button", "innerName" : "close"}]
			],
			["div", {"class" : "syslib-text syslib-dialog-body", "innerName" : "body", "style" : this.__options.bodyStyle}, 
				["div", this.__options.content]
			]
		];

		// create the object
		var objectData 	= syslib.element.createFromJSON(false, elemtJSON);
		var element		= objectData.element;
		var references	= objectData.references;
		

		// drag and drop
		if(__valid(this.__options.enableDrag)){
			new syslib.ui.Drag(references.title, element, {
				alpha: false
			});
		}
		
		// check if a content element has been informed
		try{
	 		if(this.__options.contentElement && __id(this.__options.contentElement)){
	 			// clear the body
	 			references.body.innerHTML = '';
	 			
	 			// append the content element
	 			references.body.appendChild(__id(this.__options.contentElement).cloneNode(true));
	 		}
		} catch(e){
			syslib.messages.error("syslib.ui.Dialog.createElement(): Invalid content element!");
		}
	
		// check for buttons
		if(this.__options.buttons.length > 0){
			// space
			references.body.appendChild(document.createElement("br"));
			references.body.appendChild(document.createElement("br"));
			
			// import the buttons class
			syslib.io.importClass("syslib.ui.Button", {
				onComplete : function(){
					// search the buttons
					for(var i in this.options.buttons){
						try{
							// create the button
							new syslib.ui.Button(this.options.buttons[i]).appendTo(this.references.body);
						} catch(e){
							syslib.messages.error("syslib.ui.Dialog.createElement(): Error creating the buttons: " + e.message);
						}
					}
				}.applyTo(this)
			});
		}
		
		// get the close button for set the events
		var closeBtn = references.close;
		
		// events
		__evtListen("mouseover", closeBtn, function(){
			this.references.close.className = "syslib-dialog-close-button-over";
		}.applyTo(this));
		__evtListen("mouseout", closeBtn, function(){
			this.references.close.className = "syslib-dialog-close-button";
		}.applyTo(this));
		__evtListen("click", closeBtn, function(){
			this.dismiss();
		}.applyTo(this));
		
		// store the element and the references
		this.__element = element;
		this.references = references;
		
		// return
		return element;
		
 	}, 
 	
 	append : function(){
 		// set as fullscreen, if needed
 		if(this.__options.fullscreen){
 			// get the viewport size
 			var viewportSize = syslib.ui.size.getViewportSize();
 			
 			// get the scrollbar size
 			var scrollHeight = document.body.scrollTop;
 			var scrollWidth = document.body.scrollLeft;
 			
 			// calculates the size
 			var width = viewportSize.width - (viewportSize.width * 0.05);
 			var height = viewportSize.height - (viewportSize.height * 0.05);
 			var bodyHeight = height - 30;
 			
 			// update the style
 			syslib.css.updateStyle(this.__element, {
 				"width" : width + "px", 
 				"height" : height + "px", 
 				"top" : scrollHeight, 
 				"left" : scrollWidth
 			});
 			syslib.css.updateStyle(this.references.body, {
 				height: bodyHeight + "px"
 			});
 		}
 		
 		// append
 		if(__valid(this.__options.target)){
 			this.__options.target.appendChild(this.getElement());
 		} else {
 			document.body.appendChild(this.getElement());
 		}
 	}, 
 	
 	appendChild : function(child){
 		// append the child
 		this.references.body.appendChild(child);
 	},
 	
 	show : function(){
		// append
		this.append();
 		
 		// show the body mask
 		syslib.ui.util.showBodyMask(1000);
 	
		// show the element
		__show(this.__element);
		
		// centralize
		setTimeout(function(){
			syslib.ui.position.center(this.__element);
		}.applyTo(this), 100);
 		
 		// add to the opened dialogs list
 		syslib.ui.Dialog.openedDialogs.push(this);
 	}, 
 	
 	dismiss : function(){
 		try{
 			// hide the body mask
 	 		syslib.ui.util.hideBodyMask(1000);
 	 	
 	 		// calculates a random position
 	 		x = (Math.random() * 1000) * -1;
 	 		y = (Math.random() * 1000) * -1;
 	 		if(x >= -500){ x -= 500;}
 	 		if(y >= -500){ y -= 500;}
 	 		
 	 		// dismiss
 	 		syslib.element.hide(this.__element);
 	 		
 	 		// remove from the body
 	 		this.__element.parentNode.removeChild(this.getElement());
 	 		
 	 		// remove from the opened dialogs list
 	 		var openedDialogs = syslib.ui.Dialog.openedDialogs;
 	 		for(var i in openedDialogs){
 	 			// get the dialog
 	 			var dialog = openedDialogs[i];
 	 			
 	 			// close
 	 			if(dialog.__options.id === this.__options.id){
 	 				openedDialogs.pop(dialog);
 	 			}
 	 		}
 		} catch(e){
 			syslib.messages.error("syslib.ui.Dialog.dismiss(): Error dismissing: " + e.message);
 		}
 	}
});

syslib.ui.Dialog.openedDialogs = [];
syslib.ui.Dialog.closeAllDialogs = function(){
	// get the opened dialogs
	var openedDialogs = syslib.ui.Dialog.openedDialogs;
	
	// search them
	for(var i in openedDialogs){
		// get the dialog
		var dialog = openedDialogs[i];
		
		// close
		dialog.dismiss();
	}
};